package android.longs.study.sign.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import sun.launcher.resources.launcher;

import android.longs.study.base.ResponeModel;
import android.longs.study.sign.dao.UserSignDao;
import android.longs.study.sign.dao.UserSignDaoImple;
import android.longs.study.sign.model.UserSignModel;
import android.longs.study.utils.GsonUtils;

public class UserSignServiceImple implements UserSignService {

    private UserSignDao dao = new UserSignDaoImple();
    @Override
    public String addUserSign(HttpServletRequest request) {
	ResponeModel<UserSignModel> responeModel = new ResponeModel<UserSignModel>();


	String signUserUid = request.getParameter("signUserUid");
	String signTime = request.getParameter("signTime");

	String signLocation = request.getParameter("signLocation");
	String signCity = request.getParameter("signCity");

	String signDistrict = request.getParameter("signDistrict");
	String signStreet = request.getParameter("signStreet");

	String signStreetNum = request.getParameter("signStreetNum");
	String signDescription = request.getParameter("signDescription");
	String signType = request.getParameter("signType");
	String signUserName = request.getParameter("signUserName");
	String uuid = request.getParameter("uuid");
	
	String classIdString = request.getParameter("classId");

	String result = "";

	if (signUserUid==null||signTime==null||signLocation==null||signUserUid.equals("")||signTime.equals("")||signLocation.equals("")) {

	    responeModel.code = "1002";
	    responeModel.message="签到信息不全";
	} else {

	    UserSignModel model  = new UserSignModel();
	    model.signCity = signCity;
	    model.signDescription = signDescription;
	    model.signDistrict = signDistrict;
	    
	    model.signLocation = signLocation;
	    model.signStreet = signStreet;
	    
	    model.signTime = signTime;
	    model.signUserUid = signUserUid;
	    model.signUid = uuid;
	    
	    model.signUserClassId = classIdString;
	    
	    model.signUserName = signUserName;
	    
	    model.signStreetNum = signStreetNum;
	    if (signType!=null&&!signType.equals("")) {
		 model.signType = Integer.valueOf(signType);
	    } else {
		model.signType =1;
	    }
	    
	    System.out.println(model);
	   
	    
	    UserSignModel addUserSignModel = dao.addUserSignModel(model);
	    
	    
	    responeModel.code = "1000";
	    responeModel.content = addUserSignModel;
	    responeModel.message="签到成功";
	}

	try {
	    result = GsonUtils.getintance().toJson(responeModel, null);
	} catch (Exception e) {

	}

	return result;
    }

    @Override
    public String queryUserSignFromUserId(HttpServletRequest request) {
	
	String userIdString = request.getParameter("userId");
	String index = request.getParameter("index");
	String type = request.getParameter("type");
	
	ResponeModel<List<UserSignModel>> responeModel = new ResponeModel<>();
	
	
	if (userIdString==null||userIdString.equals("")) {
	    responeModel.code="1002";
	    responeModel.code="用户ID为空";
	} else {
	    
	    if (index==null||index.equals("")) {
		index="0";
	    }
	    if (type==null||type.equals("")) {
		type="0";
	    }
	    int num = Integer.valueOf(index);
	    int typeNumber = Integer.valueOf(type);
	    List<UserSignModel> list = dao.queryAllFromUserId(userIdString, num,typeNumber);
	    responeModel.content = list;
	    responeModel.code="1000";
	    responeModel.message="请求成功";
	}
	String json = GsonUtils.getintance().toJson(responeModel, null);
	return json;
    }

    @Override
    public String queryUserSignFromId(HttpServletRequest request) {
	// TODO Auto-generated method stub
	return null;
    }

    @Override
    public String queryUserSignFromUId(HttpServletRequest request) {
	// TODO Auto-generated method stub
	return null;
    }

    @Override
    public String queryUserLastSignFromUserId(HttpServletRequest request) {
	
	String userId = request.getParameter("userId");
	String result = "";
	System.out.println("加载 最后次 ... "+userId);
	
	ResponeModel<List<UserSignModel>> responeModel = new ResponeModel<>();
	
	if (userId==null||userId.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="用户ID为空";
	    
	} else {
	    
	    UserSignModel querSignModelSignInLast = dao.querSignModelSignInLast(userId);
	    UserSignModel querSignModelSignOutLast = dao.querSignModelSignOutLast(userId);
	    
	    List<UserSignModel> list = new ArrayList<UserSignModel>();
	    if (querSignModelSignInLast!=null) {
		list.add(querSignModelSignInLast);
	    }
	    if (querSignModelSignOutLast!=null) {
		list.add(querSignModelSignOutLast);
	    }
	    responeModel.content = list;
	    responeModel.code="1000";
	    responeModel.message="加载成功";
	    
	}
	result = GsonUtils.getintance().toJson(responeModel, null);
	return result;
    }

    @Override
    public String queryUserSignFromUserClassId(HttpServletRequest request) {
	ResponeModel<List<UserSignModel>> responeModel = new ResponeModel<>();
	
	String classIdString = request.getParameter("classId");
	String indexString = request.getParameter("index");
	
	int index = 0;
	
	if (indexString==null||indexString.equals("")) {
	   index = 0;
	}
	
	if (classIdString==null||classIdString.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="班级信息为空";
	}else {
	    
	    List<UserSignModel> list = dao.queryAllFromUserClassId(classIdString, index, 20);
	    responeModel.code="1000";
	    responeModel.content = list;
	    responeModel.message="查询成功";
	}
	
	
	
	String json = GsonUtils.getintance().toJson(responeModel, null);
	return json;
    }

}
